package com.senseuni.da.utils;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.senseuni.da.MainActivity;
import com.senseuni.da.MyApplication;
import com.senseuni.da.R;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

/**
 * ================================================
 * Copyright (c) 2020 All rights reserved
 * 描述：
 * Author: Yuri
 * Date： Date: 2024/12/12
 * ================================================
 */
public class NotificationHelper {
    public static final String CHANNEL_ID = "MqttServiceChannel";
    private Context context;

    public NotificationHelper() {
        this.context = MyApplication.app.getApplicationContext();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createNotificationChannel();
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void createNotificationChannel() {
        CharSequence name = "Mqtt Service Channel";
        String description = "Channel for Mqtt Service";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            channel = new NotificationChannel(CHANNEL_ID, name, importance);
        }
        channel.setDescription(description);

        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }

    public Notification createNotification() {
        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);

        return new NotificationCompat.Builder(context, CHANNEL_ID)
                .setContentTitle("Mqtt Service")
                .setContentText("Mqtt Service is running")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pendingIntent)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .build();
    }

}
